#include"search.hpp"
#include"log.hpp"
#include"cpp-httplib/httplib.h"

const std::string input = "data/clean_html/clean.txt";
const std::string root_path = "./wwwroot";

int main()
{

    ns_search::Searcher search;
    search.InitSearch(input);

    httplib::Server ser;
    ser.set_base_dir(root_path.c_str());

    ser.Get("/s", [&search](const httplib::Request& req, httplib::Response& rsp)
    {
        if(!req.has_param("word"))
        {
            rsp.set_content("必须要有搜索关键字! ",  "text/plain; charset=utf-8");
            return;
        }

        std::string word = req.get_param_value("word");
        std::cout << "用户正在搜索: " << word << std::endl;

        std::string json_string;
        search.Search(word, &json_string);

        rsp.set_content(json_string, "application/json");
    });

    LOG(Info, "服务器启动成功!");
    ser.listen("0.0.0.0", 8899);

    return 0;
}